package com.ogawa.chair7808.view;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ogawa.chair7808.R;

/**
 * 作者: bright.lin@medisana.net.cn
 * 时间: 2018/12/19
 * 标题: 配网功能功能Dialog
 * 描述: 配网功能功能中定制的Dialog
 * 修改: 由 bright.lin@medisana.net.cn 修改于 2019/1/3
 */
public class SoftApDialog extends Dialog {

    // 单按钮
    public static final int ONE_BTN_TYPE = 1;
    // 双按钮
    public static final int TWO_BTN_TYPE = 2;

    // 类型
    private int type;
    // 提示图标
    private ImageView tipImg;
    // 提示语1
    private TextView tipOneTv;
    // 提示语2
    private TextView tipTwoTv;

    private TextView oneConfirmBtn;

    // 确认按钮回调
    private OnConfirmListener onConfirmListener;

    /**
     * 带参构造器
     *
     * @param context 上下文
     * @param type    类型
     */
    public SoftApDialog(Context context, int type) {
        super(context, R.style.SoftApDialog);
        this.type = type;
        setContentView(R.layout.layout_softap_dialog);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        initView();
    }

    /**
     * 设置提示图标
     *
     * @param resId 图标资源id
     */
    public void setTipImg(int resId) {
        tipImg.setBackgroundResource(resId);
    }

    /**
     * 设置提示语1
     *
     * @param text 提示语1
     */
    public void setTipOne(String text) {
        tipOneTv.setText(text);
    }

    /**
     * 设置提示语2
     *
     * @param text 提示语2
     */
    public void setTipTwo(String text) {
        tipTwoTv.setText(text);
    }

    public void setConfirmBtnText(String text) {
        oneConfirmBtn.setText(text);
    }

    /**
     * 初始化界面
     */
    private void initView() {
        tipImg = findViewById(R.id.softap_dialog_img);
        tipOneTv = findViewById(R.id.softap_dialog_tip_one);
        tipTwoTv = findViewById(R.id.softap_dialog_tip_two);
        View view = findViewById(R.id.softap_dialog_divider);
        oneConfirmBtn = findViewById(R.id.softap_dialog_btn);
        TextView twoConfirmBtn = findViewById(R.id.softap_dialog_btn_confirm);
        TextView twoCancelBtn = findViewById(R.id.softap_dialog_btn_cancel);
        if (type == TWO_BTN_TYPE) {
            oneConfirmBtn.setVisibility(View.INVISIBLE);
            view.setVisibility(View.VISIBLE);
            twoConfirmBtn.setVisibility(View.VISIBLE);
            twoCancelBtn.setVisibility(View.VISIBLE);
            twoConfirmBtn.setOnClickListener(v -> {
                if (null != onConfirmListener) {
                    onConfirmListener.onConfirm();
                }
            });
            twoCancelBtn.setOnClickListener(v -> dismiss());
        } else {
            oneConfirmBtn.setVisibility(View.VISIBLE);
            view.setVisibility(View.INVISIBLE);
            twoConfirmBtn.setVisibility(View.INVISIBLE);
            twoCancelBtn.setVisibility(View.INVISIBLE);
            oneConfirmBtn.setOnClickListener(v -> {
                if (null != onConfirmListener) {
                    onConfirmListener.onConfirm();
                }
            });
        }
    }

    /**
     * 设置确认按钮回调
     *
     * @param onConfirmListener 确认按钮回调
     */
    public void setOnConfirmListener(OnConfirmListener onConfirmListener) {
        this.onConfirmListener = onConfirmListener;
    }

    /**
     * 确认按钮回调接口
     */
    public interface OnConfirmListener {
        void onConfirm();
    }
}
